<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Aajax extends Controller {

	public function action_countries() {
		$res = View::factory('apieces/menu_list', array(
			'menu' => DB::select(array('country_id', 'id'), 'name')->from('country')->order_by('name', 'DESC')->as_object()->execute(),
			'nextlevel' => 'cities',
			'nextlevel_id' => $this->request->param('id'),
			'load_from' => 'country',
			//'parent_id' => $this->request->param('id')
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_cities() {
		$res = View::factory('apieces/menu_list', array(
			'menu' => DB::select(array('city_id', 'id'), 'name')->from('city')->where('country_id', '=', $this->request->param('id'))->order_by('name', 'DESC')->as_object()->execute(),
			'nextlevel' => 'hotels',
			'nextlevel_id' => $this->request->param('id'),
			'load_from' => 'city',
			//'parent_id' => $this->request->param('id')
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_hotels() {
		$res = View::factory('apieces/menu_list', array(
			'menu' => DB::select(array('id', 'id'), array('title', 'name'))->from('hotels')->where('parent', '=', $this->request->param('id'))->order_by('title', 'DESC')->as_object()->execute(),
			'nextlevel' => FALSE,
			'load_from' => 'hotel',
			//'parent_id' => ''
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_country() {
		$res = View::factory('aforms/aedit_place', array(
			'obj' => DB::select(array('country_id', 'id'), 'name', 'description', 'action', 'image')->from('country')->where('country_id', '=', $this->request->param('id'))->as_object()->execute()->current(),
			'target' => 'country',
			'new_record' => 'city',
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_city() {
		$res = View::factory('aforms/aedit_place', array(
			'obj' => DB::select(array('city_id', 'id'), 'name', 'description', 'action', 'image')->from('city')->where('city_id', '=', $this->request->param('id'))->as_object()->execute()->current(),
			'target' => 'city',
			'new_record' => 'hotel',
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_hotel() {
		$res = View::factory('aforms/aedit_place', array(
			'obj' => DB::select('id', array('title', 'name'), 'description', 'action', 'image')->from('hotels')->where('id', '=', $this->request->param('id'))->as_object()->execute()->current(),
			'target' => 'hotel',
		))->render();

		echo json_encode(array('status' => 'ok', 'data' => $res));
	}

	public function action_edit() {
		print_r($_FILES);
		if ($this->request->post('target') == 'country') {
			//$this->uploadImage($key, $obj->$key);
			
			DB::update('country')
				->set(array('name' => $this->request->post('name'), 'description' => $this->request->post('description')))
				->where('country_id', '=', $this->request->param('id'))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Изменения сохранены'));
		} elseif ($this->request->post('target') == 'city') {
			DB::update('city')
				->set(array('name' => $this->request->post('name'), 'description' => $this->request->post('description')))
				->where('city_id', '=', $this->request->param('id'))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Изменения сохранены'));
		} elseif ($this->request->post('target') == 'hotel') {
			DB::update('hotels')
				->set(array('title' => $this->request->post('name'), 'description' => $this->request->post('description')))
				->where('id', '=', $this->request->param('id'))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Изменения сохранены'));
		}
	}

	public function action_add() {
		if ($this->request->param('id') == 'country') {
			DB::insert('country', array('name', 'description'))
				->values(array($this->request->post('name'), $this->request->post('description')))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Запись добавлена'));
		} elseif ($this->request->param('id') == 'city') {
			DB::insert('city', array('country_id', 'name', 'description'))
				->values(array($this->request->post('parent_id'), $this->request->post('name'), $this->request->post('description')))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Изменения сохранены'));
		} elseif ($this->request->param('id') == 'hotel') {
			DB::insert('hotels', array('parent', 'title', 'description'))
				->values(array($this->request->post('parent_id'), $this->request->post('name'), $this->request->post('description')))
				->execute();

			echo json_encode(array('status' => 'ok', 'data' => 'Изменения сохранены'));
		}
	}

}
